package com.anjoyo.xgh.musicshare.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.anjoyo.xgh.musicshare.R;
import com.anjoyo.xgh.musicshare.adapter.NetSongAdapter;
import com.anjoyo.xgh.musicshare.api.CollectApi;
import com.anjoyo.xgh.musicshare.application.SongApplication;
import com.anjoyo.xgh.musicshare.entry.Song;
import com.anjoyo.xgh.musicshare.service.Task;
import com.anjoyo.xgh.musicshare.util.Constant;
import com.anjoyo.xgh.musicshare.util.Constant.PLAYLIST;

public class NetSongAct extends Activity implements OnClickListener {

	Context con;
	ListView listview;
	NetSongAdapter adapter;
	int currentpos = -1;
	String filepath;
	String imgurl;
	PopupWindow popupWindow;
	TextView tvclanguage;
	SongApplication application;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == Constant.NETSONG_DOWLOAD_OK) {
				adapter.notifyDataSetChanged();
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.netsong_layout);
		con = this;
		initPopWindow();
		application = SongApplication.getInstance();
		listview = (ListView) findViewById(R.id.lv_netsong);
		if (application.songslist.size() < 1) {
			application.LoadNetSongs(20, "", handler);
			System.out.println("数据要下载");
		}
		tvclanguage = (TextView) findViewById(R.id.tv_clanguage);
		tvclanguage.setOnClickListener(this);
		findViewById(R.id.iv_menu).setOnClickListener(this);
		findViewById(R.id.iv_music).setOnClickListener(this);
		adapter = new NetSongAdapter(con);
		listview.setAdapter(adapter);
		initListener();
		SongApplication.getInstance().changNowPlayList(PLAYLIST.NET_SONGS_LIST);

	}

	public void initPopWindow() {
		View view = LayoutInflater.from(con).inflate(
				R.layout.pop_langage_layout, null);
		popupWindow = new PopupWindow(view, 160, 360);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setOutsideTouchable(true);
		popupWindow.setTouchInterceptor(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					if (popupWindow.isShowing()) {
						popupWindow.dismiss();
					}
				}
				return false;
			}
		});
		Onclickl onclickl = new Onclickl();
		view.findViewById(R.id.tv_language1).setOnClickListener(onclickl);
		view.findViewById(R.id.tv_language2).setOnClickListener(onclickl);
		view.findViewById(R.id.tv_language3).setOnClickListener(onclickl);
		view.findViewById(R.id.tv_language4).setOnClickListener(onclickl);
		view.findViewById(R.id.tv_language5).setOnClickListener(onclickl);
		view.findViewById(R.id.tv_language6).setOnClickListener(onclickl);
		view.findViewById(R.id.tv_language7).setOnClickListener(onclickl);
	}

	private void initListener() {
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				adapter.changeColor(arg2);
				currentpos = arg2;
				Intent intent = new Intent(Constant.ACTION_PLAY);
				intent.putExtra("pos", currentpos);
				sendBroadcast(intent);
			}
		});

		listview.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				adapter.changeColor(arg2);
				currentpos = arg2;
				new AlertDialog.Builder(con)
						.setSingleChoiceItems(
								new String[] { "下载", "收藏", "分享" }, 0,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										if (which == 0) {
											dialog.dismiss();
											Song song = (Song) adapter
													.getItem(currentpos);
											// System.out.println(song.toString());
											Task task = new Task();
											task.name = song.getName();
											task.obj = song;
											task.type = Constant.DOWN_TYPE.MP3;

											Intent intent = new Intent(
													Constant.SERVICE_DOWN);
											intent.putExtra("task", task);
											sendBroadcast(intent);
										} else if (which == 1) {
											dialog.dismiss();
											Song song = (Song) adapter
													.getItem(currentpos);
											CollectApi api = new CollectApi();
											api.clodesong(con, song);
										} else if (which == 2) {
											dialog.dismiss();
											Song song = (Song) adapter
													.getItem(currentpos);
											Intent intent = new Intent(con,
													ShareAct.class);
											intent.putExtra("song", song);
											startActivity(intent);
										}
									}
								}).setNegativeButton("关闭", null).create()
						.show();
				return false;
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_clanguage:
			popupWindow.showAsDropDown(tvclanguage);
			break;
		case R.id.iv_music:
			Intent intent = new Intent(con, PlayAct.class);
			startActivity(intent);
			break;
		case R.id.iv_menu:
			finish();
			break;
		default:
			break;
		}
	}

	class Onclickl implements OnClickListener {

		@Override
		public void onClick(View v) {
			if (popupWindow.isShowing()) {
				popupWindow.dismiss();
				adapter.pos = -1;
			}
			String language = ((TextView) v).getText().toString();
			application.LoadNetSongs(30, language, handler);
		}
	}
}
